package com.FH.applyForm.officeServies.expenform_name;

import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/10/21.
 */
@Stateless
public class ExpenformnameDAOImpl extends AccessDB implements ExpenformnameDAO{

    /**
     * 查询报销单多类别信息
     * @param search
     * @param page
     * @param size
     * @return
     */
    @Override
    public ResultDTO getExpenformNameInfo(String search, String page, String size) {
        ResultDTO resultDTO = new ResultDTO();
        try {
            JSONObject json = new JSONObject(search);
            String existName = "0";

            String sqlexit = "select count(1) from expen_type where 1=1 ";

            String sql_w = "";//查询条件sql组合

            String sql_page = "";//分页sql组合
            Integer page_ok = Integer.parseInt(page);
            Integer size_ok = Integer.parseInt(size);
            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok) + "," + size + "";
            }
            if (json.has("expen_form_id")) {
                sql_w += " AND expen_form_id = '" + json.getString("expen_form_id") + "'";
            }
            String sql_ok = sqlexit + sql_w;//组合好的sql语句
            existName = getReportingToOne(sql_ok);

            List<String[]> seresult = new ArrayList<String[]>();

            List<ExpenformnameShowDTO> result = new ArrayList<ExpenformnameShowDTO>();
            if (!existName.equals("0")) {
                String sql = "select id,advancetypess_name,expen_form_id,advancetypess_price,advancetypess_id,advance_starttime,advance_desciption,advance_endtime from expen_type  where 1=1" + sql_w +  sql_page;
                seresult = this.getReportingTo(sql);

                //构建返回对象
                for (int i = 0; i < seresult.size(); i++) {

                    ExpenformnameShowDTO advanceShowDTO = new ExpenformnameShowDTO();
                    advanceShowDTO.setId(seresult.get(i)[1]);
                    advanceShowDTO.setAdvancetypess_name(seresult.get(i)[2]);
                    advanceShowDTO.setExpen_form_id(seresult.get(i)[3]);
                    advanceShowDTO.setAdvancetypess_price(seresult.get(i)[4]);
                    advanceShowDTO.setAdvancetypess_id(seresult.get(i)[5]);
                    advanceShowDTO.setAdvance_starttime(seresult.get(i)[6]);
                    advanceShowDTO.setAdvance_desciption(seresult.get(i)[7]);
                    advanceShowDTO.setAdvance_endtime(seresult.get(i)[8]);

                    result.add(advanceShowDTO);
                }
            }
            if (seresult.size() != 0) {
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            } else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }
            resultDTO.setStr(result.size() + "");
            resultDTO.setValues(result);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return resultDTO;

    }

    /**
     * 添加报销单多类别
     * @param expenformnamePullDTO
     * @return
     */
    @Override
    public ResultDTO addExpenformNameInfo(ExpenformnamePullDTO expenformnamePullDTO) {
        int resultls=0;
        ResultDTO resultDTO=new ResultDTO();

        String uuid= UUID.randomUUID().toString().trim().replaceAll("-", "");


        String Orgrole_insert="";
        Orgrole_insert="insert into expen_type (id,advancetypess_name,expen_form_id,advancetypess_price,advancetypess_id,advance_starttime,advance_desciption,advance_endtime)" +
                "values('"+uuid+"','"+expenformnamePullDTO.getAdvancetypess_name()+"','"+expenformnamePullDTO.getExpen_form_id()+"','"+expenformnamePullDTO.getAdvancetypess_price()+"','"+expenformnamePullDTO.getAdvancetypess_id()+"','"+expenformnamePullDTO.getAdvance_starttime()+"','"+expenformnamePullDTO.getAdvance_desciption()+"','"+expenformnamePullDTO.getAdvance_endtime()+"')";

        resultls=addReportingTo(Orgrole_insert);
        if(resultls!=0){
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.add_success);
        }else{
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.add_fail);
        }
        return resultDTO;
    }

    /**
     * 修改报销单多类别
     * @param expenformnamePullDTO
     * @return
     */
    @Override
    public ResultDTO updateExpenformNameInfo(ExpenformnamePullDTO expenformnamePullDTO) {
        ResultDTO resultDTO = new ResultDTO();
        String QUERY = "";
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //更新sql语句
        QUERY += "update expen_type set ";
        if(null!=expenformnamePullDTO.getAdvancetypess_name()&&!"".equals(expenformnamePullDTO.getAdvancetypess_name())){
            QUERY += " advancetypess_name = '" + expenformnamePullDTO.getAdvancetypess_name() + "'";
        }

        if(null!=expenformnamePullDTO.getExpen_form_id()&&!"".equals(expenformnamePullDTO.getExpen_form_id())){
            QUERY += ",expen_form_id ='" + expenformnamePullDTO.getExpen_form_id() + "'";
        }

        if(null!=expenformnamePullDTO.getAdvancetypess_number()&&!"".equals(expenformnamePullDTO.getAdvancetypess_number())){
            QUERY += ",advancetypess_number ='" + expenformnamePullDTO.getAdvancetypess_number() + "'";
        }

        if(null!=expenformnamePullDTO.getAdvancetypess_id()&&!"".equals(expenformnamePullDTO.getAdvancetypess_id())){
            QUERY += ",advancetypess_id ='" + expenformnamePullDTO.getAdvancetypess_id() + "'";
        }

        if(null!=expenformnamePullDTO.getAdvancetypess_price()&&!"".equals(expenformnamePullDTO.getAdvancetypess_price())){
            QUERY += ",advancetypess_price ='" + expenformnamePullDTO.getAdvancetypess_price() + "'";
        }
        if(null!=expenformnamePullDTO.getAdvance_starttime()&&!"".equals(expenformnamePullDTO.getAdvance_starttime())){
            QUERY += ",advance_starttime ='" + expenformnamePullDTO.getAdvance_starttime() + "'";
        }
        if(null!=expenformnamePullDTO.getAdvance_desciption()&&!"".equals(expenformnamePullDTO.getAdvance_desciption())){
            QUERY += ",advance_desciption ='" + expenformnamePullDTO.getAdvance_desciption() + "'";
        }
        if(null!=expenformnamePullDTO.getAdvance_endtime()&&!"".equals(expenformnamePullDTO.getAdvance_endtime())){
            QUERY += ",advance_endtime ='" + expenformnamePullDTO.getAdvance_endtime() + "'";
        }



        QUERY += " where expen_form_id = '" + expenformnamePullDTO.getExpen_form_id() + "'";


        int result = this.addReportingTo(QUERY);

        // 判断SQl是否执行成功
        if (result != 0) {
            resultDTO.setStatus("90000");//
            resultDTO.setDescritpion(warnSrc.update_success);
        } else {
            resultDTO.setStatus("90001");//
            resultDTO.setDescritpion(warnSrc.update_fail);
        }
        return resultDTO;
    }

    /**
     * 删除报销单多类别
     * @param expenformnamePullDTO
     * @return
     */
    @Override
    public ResultDTO deleteExpenformNameInfo(ExpenformnamePullDTO expenformnamePullDTO) {
        String sql="select count(1) from expen_type WHERE expen_form_id='"+expenformnamePullDTO.getExpen_form_id()+"'";
        String existadvance= getReportingToOne(sql);

        int result = 0;

        ResultDTO resultDTO = new ResultDTO();
        // 删除SQL语句
        if(!existadvance.equals("0")){
            String QUERY = "";
            QUERY += "delete from expen_type  where expen_form_id='"+expenformnamePullDTO.getExpen_form_id()+"'";

            result = this.addReportingTo(QUERY);
        }

        // 判断SQl是否执行成功
        if (result!= 0) {

            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.delete_success);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.delete_fail);
        }
        return resultDTO;
    }
}
